<?php

require_once BaseUtil::getModulePath("base")  .  '/forms/BaseForm.php';
require_once BaseUtil::getModulePath("base")  .  '/validate/Unique.php';

class CustomerForm extends BaseForm {
    public function __construct ($edit = false , $customerId=null)
    {
        parent::__construct();
        if($edit){
	       $this->setAction(HttpUtil::getUrl("customer", "subedit"));
        }else{
           $this->setAction(HttpUtil::getUrl("customer", "subadd"));
        }

        //id
        if($edit){
            $id = new Zend_Form_Element_Hidden('id');
            $this->addElement($id);
        }
        
        //unique name
        $validNameUnique = new MyValid_Unique();
        $validNameUnique->setTableName('users');
        $validNameUnique->setFieldName('name');
        if(!empty($customerId)){
            $validNameUnique->setId($customerId);
        }

        //unique validate
        $validUnique = new MyValid_Unique();
        $validUnique->setTableName('users');
        $validUnique->setFieldName('email');
        if(!empty($customerId)){
            $validUnique->setId($customerId);
        }
        
        //customer name
        $name = new Zend_Form_Element_Text('name');
        $name->setLabel($this->translate->_('name'));
        $name->addFilter("StringTrim");
        $name->setRequired(true);
        $name->addValidator($validNameUnique);
        $name->setAttrib('id','username');

        //customer email
        $email = new Zend_Form_Element_Text('email');
        $email->setLabel($this->translate->_('email'));
        $email->addFilter("StringTrim");
        $email->setRequired(true);
        $email->addValidator("EmailAddress");
        $email->addValidator($validUnique);
        $email->setAttrib('id','emailbox');

        //customer password
        $password = new Zend_Form_Element_Password('password');
        $password->setLabel($this->translate->_('password'));
        $password->addFilter("StringTrim");
        if(!$edit){
            $password->setRequired(true);
        }
        $password->setAttrib('id','password');
        
        $userType = new Zend_Form_Element_Select('userType');
        $userType->setLabel($this->translate->_('userType'));
        $typeArr = array('1'=>$this->translate->_('certf1'),'3'=>$this->translate->_('certf3'),'4'=>$this->translate->_('certf4'),
                         '5'=>$this->translate->_('certf5'),'6'=>$this->translate->_('certf6'));
        $userType->addMultiOptions($typeArr);
      //  $userType->setRequired(true);
        $userType->setAttrib('onchange','Customer.changeType()');
        $userType->setAttrib('id','userType');
        
        $team = new Zend_Form_Element_Text('teamName');
        $team->setLabel($this->translate->_('teamName'));
        $team->addFilter("StringTrim");
     
        $team->setAttrib('id','teamName');
        
        $fullName = new Zend_Form_Element_Text('fullName');
        $fullName->setLabel($this->translate->_('fullName'));
        $fullName->addFilter("StringTrim");
     
        $fullName->setAttrib('id','fullName');
        
        $province = new Zend_Form_Element_Text('province');
        $province->setLabel($this->translate->_('province'));
        $province->addFilter("StringTrim");
    
        $province->setAttrib('id','province');
        
        $city = new Zend_Form_Element_Text('city');
        $city->setLabel($this->translate->_('city'));
        $city->addFilter("StringTrim");
    
        $city->setAttrib('id','city');
        
        $receiveAddr = new Zend_Form_Element_Textarea('receiveAddr');
        $receiveAddr->setAttribs(array("rows" => 3, "cols" => 35));
        $receiveAddr->setLabel($this->translate->translate("receiveAddr"));
   
        $receiveAddr->setAttrib('id','address');
        
        $zipCode = new Zend_Form_Element_Text('zipCode');
        $zipCode->setLabel($this->translate->_('zipCode'));
        $zipCode->addFilter("StringTrim");
    
        $zipCode->setAttrib('id','zipCode');
        
        $mobile = new Zend_Form_Element_Text('mobile');
        $mobile->setLabel($this->translate->_('mobileNum'));
        $mobile->addFilter("StringTrim");
  
        $mobile->setAttrib('id','mobile');
        
        $phone = new Zend_Form_Element_Text('phone');
        $phone->setLabel($this->translate->_('phoneNum'));
        $phone->addFilter("StringTrim");
        $phone->setAttrib('id','phone');
        
        
        //customer status
        if($edit){
        	$lastIp = new Zend_Form_Element_Text('lastIp');
            $lastIp->setLabel($this->translate->_('lastIp'));
            $lastIp->addFilter("StringTrim");
            $lastIp->setAttrib('disable','disable');
            $lastIp->setAttrib('style','background-color:#fff; color:#606060;');
        	$this->addElement($lastIp);
        	
        	$lastLogin = new Zend_Form_Element_Text('lastLogin');
            $lastLogin->setLabel($this->translate->_('lastLogin'));
            $lastLogin->addFilter("StringTrim");
            $lastLogin->setAttrib('disable','disable');
            $lastLogin->setAttrib('style','background-color:#fff; color:#606060;');
        	$this->addElement($lastLogin);
        	
        	$pointsBuyer = new Zend_Form_Element_Text('pointsBuyer');
            $pointsBuyer->setLabel($this->translate->_('pointsBuyer'));
            $pointsBuyer->addFilter("StringTrim");
            $pointsBuyer->setAttrib('id','pointsBuyer');
        	$this->addElement($pointsBuyer);
        	
        	/*$pointsSeller = new Zend_Form_Element_Text('pointsSeller');
            $pointsSeller->setLabel($this->translate->_('pointsSeller'));
            $pointsSeller->addFilter("StringTrim");
            $pointsSeller->setAttrib('id','pointsSeller');
        	$this->addElement($pointsSeller);*/
        	
        	$pointsBuyerTotal = new Zend_Form_Element_Text('pointsBuyerTotal');
            $pointsBuyerTotal->setLabel($this->translate->_('pointsBuyerTotal'));
            $pointsBuyerTotal->addFilter("StringTrim");
            $pointsBuyerTotal->setAttrib('id','pointsBuyerTotal');
        	$this->addElement($pointsBuyerTotal);
        	
        	/*$pointsSellerTotal = new Zend_Form_Element_Text('pointsSellerTotal');
            $pointsSellerTotal->setLabel($this->translate->_('pointsSellerTotal'));
            $pointsSellerTotal->addFilter("StringTrim");
            $pointsSellerTotal->setAttrib('id','pointsSellerTotal');
        	$this->addElement($pointsSellerTotal);*/
        	
        	$customerRank = new Zend_Form_Element_Text('customerRank');
        	$customerRank->setLabel($this->translate->_('customerRank'));
        	$customerRank->setAttrib('disable','disable');
        	$customerRank->setAttrib('style','background-color:#fff; color:#606060;');
        	$this->addElement($customerRank);
        	
            $status = new Zend_Form_Element_Select('status');
            $status->setLabel($this->translate->_('status'));
            $status->setMultiOptions(array("1"=> $this->translate->_('active') ,"0"=> $this->translate->_('unactivated'),"2" => $this->translate->_('suspend')));
            $this->addElement($status);
        }

        $this->addElements(array($name,$password,$email,$userType,$team,$fullName,$province,$city,$receiveAddr,$zipCode,$mobile,$phone));

        $this->setElementDecorators(array(
            'ViewHelper',
            array(array('div' => 'HtmlTag'), array('tag' => 'div',"class" => "form_element")),
        ));
    }
}